﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace FigureDraw
{
    public class GDIPlusAdaptee
    {


        public GDIPlusAdaptee()
        {
            // TODO: Complete member initialization
        }


        public void DrawLine(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            panelDraw=new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = panelDraw.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            myGraphics.DrawLine(myPen, p1, p2);

            myPen.Dispose();
            myGraphics.Dispose();
        }

        public void DrawImage(Paper drawPaper, Vector2 oldP, Vector2 curP, string path)
        {
            panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }

            Graphics myGraphics = panelDraw.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            Image tempImage = System.Drawing.Image.FromFile("/Shape/"+path);
            Rectangle rect = new Rectangle();
            rect.X = (p1.X > p2.X ? p2.X : p1.X);
            rect.Y = (p1.Y > p2.Y ? p2.Y : p1.Y);
            rect.Width = Math.Abs(p1.X - p2.X);
            rect.Height = Math.Abs(p1.Y - p2.Y);

            myGraphics.DrawImage(tempImage, rect);
            myPen.Dispose();
            myGraphics.Dispose();
        }

        internal void DrawEclipse(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = panelDraw.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            Rectangle rect = new Rectangle();
            rect.X = (p1.X > p2.X ? p2.X : p1.X);
            rect.Y = (p1.Y > p2.Y ? p2.Y : p1.Y);
            rect.Width = Math.Abs(p1.X - p2.X);
            rect.Height = Math.Abs(p1.Y - p2.Y);
            myGraphics.DrawEllipse(myPen, rect);
        }

        private Panel panelDraw;
        internal void DrawRectangle(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = panelDraw.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            Rectangle rect = new Rectangle();
            rect.X = (p1.X > p2.X ? p2.X : p1.X);
            rect.Y = (p1.Y > p2.Y ? p2.Y : p1.Y);
            rect.Width = Math.Abs(p1.X - p2.X);
            rect.Height = Math.Abs(p1.Y - p2.Y);

            myGraphics.DrawRectangle(myPen, rect);
        }

        internal void SavePNG(string filePath)
        {
            Bitmap bmpPicture = new Bitmap(panelDraw.Width, panelDraw.Height);
            Graphics g = Graphics.FromImage(bmpPicture);
            Rectangle rect = panelDraw.RectangleToScreen(panelDraw.ClientRectangle);

            g.CopyFromScreen(rect.Location, Point.Empty, panelDraw.Size);

            bmpPicture.Save(filePath);
        }

        internal void SaveJPG(string filePath)
        {
            Bitmap bmpPicture = new Bitmap(panelDraw.Width, panelDraw.Height);
            Graphics g = Graphics.FromImage(bmpPicture);
            Rectangle rect = panelDraw.RectangleToScreen(panelDraw.ClientRectangle);

            g.CopyFromScreen(rect.Location, Point.Empty, panelDraw.Size);

            bmpPicture.Save(filePath);
        }

        internal void DrawArrow(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            panelDraw = new Panel();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(panelDraw))
            {
                panelDraw = (Panel)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = panelDraw.CreateGraphics();
            Pen myPen = new Pen(Color.Black);

            GraphicsPath capPath = new GraphicsPath();
            capPath.AddLine(-20, 0, 20, 0);
            capPath.AddLine(-20, 0, 0, 20);
            capPath.AddLine(0, 20, 20, 0);
            myPen.CustomEndCap = new System.Drawing.Drawing2D.CustomLineCap(null, capPath);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            myGraphics.DrawLine(myPen, p1, p2);

            myPen.Dispose();
            myGraphics.Dispose();
        }
    }
}
